package com.shycloud.mido.member.controller;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shycloud.mido.common.core.constant.CommonConstants;
import com.shycloud.mido.common.core.exception.BusinessException;
import com.shycloud.mido.common.core.util.R;
import com.shycloud.mido.common.security.util.SecurityUtils;
import com.shycloud.mido.member.api.entity.*;
import com.shycloud.mido.member.api.vo.VipMemberVO;
import com.shycloud.mido.member.mapper.BasicGoodsInfoMapper;
import com.shycloud.mido.member.mapper.MemberMapper;
import com.shycloud.mido.member.mapper.SanboxVipPayMemberMapper;
import com.shycloud.mido.member.service.*;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

/**
 * vip用户管理模块
 *
 * @author nianhua.jiang
 * @description
 * @date 2020/7/6
 */
@RestController
@AllArgsConstructor
@RequestMapping("vip")
@ApiIgnore
public class VipController {

	/** vip 会员模块 service */
	private final VipMemberService vipMemberService;
	/** 会员比例 service */
	private final SmMemberPriceService memberPriceService;
	/** 虚拟币充值比例 service */
	private final SmRechargeRatioService rechargeRatioService;
	/** 会员模块 service */
	private final MemberServiceV2 memberServiceV2;

	private final MemberMapper memberMapper;
	/** 商品模块 Mapper */
	private final BasicGoodsInfoMapper goodsInfoMapper;
	/** 沙盒账号业务 mapper */
	private final SanboxVipPayMemberMapper sanboxVipPayMemberMapper;

	/**
	 * VIP 会员信息获取
	 *
	 * @param
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/7/6 15:22
	 */
	@GetMapping("getVIPInfo")
	@ApiOperation("VIP 会员信息获取")
	public R getVIPInfo(String version) {

		//初始化结构集
		Map<String, Object> resultMap = MapUtil.newHashMap();

		//获取用户的ID
		String memberId = SecurityUtils.getMember().getId();

		//vip用户信息
		VipMemberVO vipMemberVO = vipMemberService.getVipMemberDetail(memberId);
		//vip套餐信息
		List<SmMemberPrice> list = null;

		if (StringUtils.isEmpty(version)) {
			list = memberPriceService.list(new QueryWrapper<SmMemberPrice>().eq("version", "1").orderByAsc(SmMemberPrice.COL_SORT));
		} else {
			if (sanboxVipPayMemberMapper.selectCount(new QueryWrapper<SanboxVipPayMember>()
					.eq("username", vipMemberVO.getUsername())) > 0) {
				list = memberPriceService
						.list(new QueryWrapper<SmMemberPrice>().eq("version", "sanBox").orderByAsc(SmMemberPrice.COL_SORT));
			} else {
				list = memberPriceService
						.list(new QueryWrapper<SmMemberPrice>().eq("version", version).orderByAsc(SmMemberPrice.COL_SORT));
			}
		}

		//用户信息
		resultMap.put("userDetail", vipMemberVO);
		//会员套餐价格
		resultMap.put("vipPackage", list);

		return new R(resultMap);
	}

	/**
	 * 获取虚拟币充值比例
	 *
	 * @param
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/7/9 9:43
	 */
	@GetMapping("getRechargeRatio")
	@ApiOperation("获取虚拟币充值比例")
	public R getRechargeRatio() {
		return new R(rechargeRatioService.list());
	}

	/**
	 * 续费会员
	 *
	 * @param
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/7/29 17:28
	 */
	@GetMapping("renewVipMember")
	@ApiOperation("续费会员")
	public R renewVipMember(Integer vipPackageId) {

		//根据会员套餐ID查询用户要花费多少虚拟币
		SmMemberPrice memberPrice = memberPriceService.getById(vipPackageId);

		//获取用户信息，查询用户剩余虚拟币数量
		Member member = memberServiceV2.getById(SecurityUtils.getMember().getId());

		if (memberPrice.getPriceNow() > member.getGold()) {
			return new R(CommonConstants.FAIL, "您的账户余额不足，请充值", false);
		}
		return new R(vipMemberService.renewVipMember(memberPrice, member));
	}

	//下面是后台接口
	@GetMapping("/getRechargeList")
	@ApiOperation("后台获取充值比例")
	public R getRechargeList() {
		return new R(rechargeRatioService.list());
	}

	@GetMapping("/getVipriceList")
	@ApiOperation("后台获取会员价格")
	public R getViprivceList() {
		return new R(memberPriceService.list(new QueryWrapper<SmMemberPrice>().eq("version", 2).orderByAsc(SmMemberPrice.COL_SORT)));
	}

	@PostMapping("/updateRechargeById")
	@ApiOperation("修改充值比例")
	public R updateRechargeById(@RequestBody SmRechargeRatio entity) {
		return new R(rechargeRatioService.updateById(entity));
	}

	@PostMapping("/updateVipriceById")
	@ApiOperation("修改会员价格")
	public R updateVipriceById(@RequestBody SmMemberPrice entity) {
		return new R(memberPriceService.updateById(entity));
	}

	@GetMapping("/updateCollectionId")
	public R updateCollectionId() {
		List<Map<String, Integer>> list = memberMapper.getOldIdList();
		for (Map<String, Integer> map : list) {
			memberMapper.updateIdOldToNew(map.get("new_id"), map.get("old_id"));
		}
		return new R<>(list);
	}

	@GetMapping("/getSanBoxUser")
	public R getSanBoxUser() {
		return new R(sanboxVipPayMemberMapper.selectList(new QueryWrapper<>()));
	}

	@GetMapping("/delSanBoxUser")
	public R delSanBoxUser(String username) {
		return new R(sanboxVipPayMemberMapper.delete(new QueryWrapper<SanboxVipPayMember>().eq("username", username)));
	}

	@GetMapping("/addSanBoxUser")
	public R addSanBoxUser(String username) {
		SanboxVipPayMember member = new SanboxVipPayMember();
		member.setUsername(username);
		return new R(sanboxVipPayMemberMapper.insert(member));
	}

	//******************* 教辅功能 **********************//

	/**
	 * 获取教辅价格
	 *
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2021/2/7 10:42
	 **/
	@GetMapping("getAssistPermissionInfo")
	public R getAssistPermissionInfo() {

		//获取教辅权限对应的价格信息，注：虚拟商品【教辅权限】的 productId 固定为11
		List<BasicGoodsInfo> basicGoodsInfos = goodsInfoMapper.selectList(new QueryWrapper<BasicGoodsInfo>()
				.eq("product_id", 11)
				.eq("del_flag", 0)
				.eq("status", "0")
				.orderByAsc("sort"));

		return new R(basicGoodsInfos);
	}

	@ApiOperation("feign用开通/续费用户VIP")
	@PostMapping("reGiveVip")
	public R reGiveVip(@RequestBody Map reqMap) throws BusinessException {

		//运营商ID
		Integer optId = (Integer) reqMap.get("optId");
		//用户ID
		String memberId = (String) reqMap.get("memberId");
		//开通时长
		Integer duration = (Integer) reqMap.get("duration");

		return new R(vipMemberService.renewVipMember(optId, duration, memberId));

	}


}
